package leetcode.easy.q06;

import java.util.ArrayList;
import java.util.List;

class Solution1 {
    public String convert(String s, int numRows) {
        if (numRows == 1) {
            return s;
        }
        char[] ans = new char[s.length()];
        int cnt = numRows * 2 - 2;
        char[] chars = s.toCharArray();
        //每一t会消耗2n-2个元素
        //s.index=t*(2*numRows-2)+j
        int maxT = chars.length % cnt == 0 ? chars.length / cnt : chars.length / cnt + 1;
        for (int j = 0, index = 0; j < numRows; j++) {
            for (int t = 0; t < maxT && (t * cnt + j) < chars.length; t++) {
                ans[index++] = chars[(t * cnt + j)];
                if (j != 0 && j != numRows - 1 && (t * cnt + cnt - j) < chars.length) {
                    ans[index++] = chars[(t * cnt + cnt - j)];
                }
            }
        }

        return String.valueOf(ans);
    }

    public static void main(String[] args) {
//        System.out.println(String.format("%8s","c"));
//        System.out.println(String.format("%7s","c"));
//        System.out.println(String.format("%6s","c"));
//        System.out.println(String.format("%12c",'c'));
        System.out.println(new Solution1().convert("PAYPALISHIRING",
                4
        ));
    }
}